#include "../Include/Configuration.h"
#include <fstream>
#define LOGGER_LEVEL LL_WARN

// using namespace std;

Configuration* Configuration::_pInstance = nullptr;  // 懒汉模式
std::string Configuration::_filepath = "";           // 懒汉模式
pthread_once_t Configuration::_once = PTHREAD_ONCE_INIT;

Configuration::Configuration() {
    readConfigFile();
    readEnglishStopWordFile();
    readChineseStopWordFile();
}

Configuration::~Configuration() {}

void Configuration::init() {
    _pInstance = new Configuration;
    atexit(destroy);
}

void Configuration::destroy() {
    if (_pInstance) {
        delete _pInstance;
        _pInstance = nullptr;
    }
}

Configuration& Configuration::getInstance() {
    _filepath = "../conf/myconf.conf";
    pthread_once(&_once, init);
    return *_pInstance;
}

std::map<std::string, std::string>& Configuration::getConfigMap() {
    return _configMap;
}

std::set<std::string>& Configuration::getEnglishStopWordList() {
    return _englishStopWordList;
}

std::set<std::string>& Configuration::getChineseStopWordList() {
    return _chineseStopWordList;
}

std::string Configuration::getWebXmlPath() {
    std::string path = _configMap["WEBXMLS_Path"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}

std::string Configuration::getFormatedWebPagePath() {
    std::string path = _configMap["Formated_WebPage_Path"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}

std::string Configuration::getOffsetFilePath() {
    std::string path = _configMap["Offset_File_Path"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}

std::string Configuration::getInvertIndxPath() {
    std::string path = _configMap["InvertIndex_Path"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}


//读取mysql相关信息
std::string Configuration::getMysqlIp() {
    std::string path = _configMap["MysqlIp"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlUsername() {
    std::string path = _configMap["MysqlUsername"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlPassword() {
    std::string path = _configMap["MysqlPassword"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlDbname() {
    std::string path = _configMap["MysqlDbname"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlPort() {
    std::string path = _configMap["MysqlPort"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlMinsize() {
    std::string path = _configMap["MysqlMinsize"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlMaxsize() {
    std::string path = _configMap["MysqlMaxsize"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlTimeout() {
    std::string path = _configMap["MysqlTimeout"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}
std::string Configuration::getMysqlMaxIdleTime() {
    std::string path = _configMap["MysqlMaxIdleTime"];
    if (path.empty()) {
        LogError("Get Path ERROR!");
    }
    return path;
}








int Configuration::getSimhashTopk() {
    std::string tmp = _configMap["Simhash_Topk"];
    if (tmp.empty()) {
        LogError("Get tmp ERROR!");
    }
    return atoi(tmp.c_str());
}

int Configuration::getSimhashEqualFlag() {
    std::string tmp = _configMap["Simhash_Equal_Flag"];
    if (tmp.empty()) {
        LogError("Get tmp ERROR!");
    }
    return atoi(tmp.c_str());
}

void Configuration::readConfigFile() {
    LogInfo("start  readConfigFile()");
    std::ifstream configFile(_filepath);
    if (!configFile.is_open()) {
        std::cerr << "Error opening configuration file: " << _filepath
                  << std::endl;
        return;
    }

    std::string line;
    while (std::getline(configFile, line)) {
        if (line.empty() || line[0] == '#') {
            continue;  // Skip empty lines and comments
        }

        size_t delimiterPos = line.find('=');
        if (delimiterPos != std::string::npos) {
            std::string key = line.substr(0, delimiterPos);
            std::string value = line.substr(delimiterPos + 1);
            _configMap[key] = value;
        }
    }

    configFile.close();
}

void Configuration::readEnglishStopWordFile() {
    std::string stopWordFilePath = _configMap["EnglishStopWord_File_Path"];
    if (stopWordFilePath.empty()) {
        std::cerr << "Error: stopword_file not specified in configuration"
                  << std::endl;
        return;
    }

    std::ifstream stopWordFile(stopWordFilePath);
    if (!stopWordFile.is_open()) {
        std::cerr << "Error opening stopword file: " << stopWordFilePath
                  << std::endl;
        return;
    }

    std::string word;
    while (stopWordFile >> word) {
        _englishStopWordList.insert(word);
    }

    stopWordFile.close();
}

void Configuration::readChineseStopWordFile() {
    std::string stopWordFilePath = _configMap["ChineseStopWord_File_Path"];
    if (stopWordFilePath.empty()) {
        std::cerr << "Error: stopword_file not specified in configuration"
                  << std::endl;
        return;
    }

    std::ifstream stopWordFile(stopWordFilePath);
    if (!stopWordFile.is_open()) {
        std::cerr << "Error opening stopword file: " << stopWordFilePath
                  << std::endl;
        return;
    }

    std::string word;
    while (stopWordFile >> word) {
        _chineseStopWordList.insert(word);
    }

    stopWordFile.close();
}